package netty.test14Hreats;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;

import java.util.Date;

public class MyClientIdleHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        if (evt instanceof IdleStateEvent) {
            IdleStateEvent e = (IdleStateEvent) evt;
            if (e.state() == IdleState.READER_IDLE) {

                ctx.close(); //Closed the Channel so that the `channelInactive` will be trigger

            } else if (e.state() == IdleState.WRITER_IDLE) {

                ctx.writeAndFlush("ping\n"); //Send ping to client

            }
        }
    }
}
